import requests
import random
import time

count = 0
# API地址和基本信息
base_url = 'http://124.223.139.46:30001/api'
headers = {
    'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
}

timeout = 48 * 60 * 60

# 打开error.txt文件，准备写入错误信息
error_file = open('error.txt', 'w', encoding='utf-8')

# 循环运行直到超时
start_time = time.time()
while time.time() - start_time < timeout:
    try:
        # 生成随机手机号
        phone = f"{random.randint(10000000000, 99999999999)}"

        # 接口1：注册短信发送
        response = requests.post(
            f"{base_url}/users",
            headers=headers,
            data={'phone': phone}
        )
        response_data = response.json()

        if response.status_code == 200 and response_data.get('send') is True:

            # 接口2：注册
            response = requests.post(
                f"{base_url}/user/verify",
                headers=headers,
                json={
                    "country": "Nigeria",
                    "first_name": "234",
                    "invite_code": "invicode",
                    "last_name": "567",
                    "password": "qwe123",
                    "promo_code": "",
                    "type": "phone",
                    "value": phone,
                    "verification_code": "9999"
                }
            )
            response_data = response.json()

            if response.status_code == 200:
                # 获取access_token
                access_token = response_data.get('auth', {}).get('access_token')

                # 接口3：访问钱包
                response = requests.get(
                    f"{base_url}/v2/user/wallet",
                    params={'user_id': response_data.get('id')},
                    headers={'Authorization': f'Bearer {access_token}'}
                )

                if response.status_code != 200:
                    error_message = f"接口3 - 钱包访问失败,状态码{response.status_code}"
                    error_file.write(error_message + '\n')
                count += 1
                print(f"第{count}条数据执行完成~")
                print(phone)
                # time.sleep(6)

            else:
                error_message = "接口2 - 注册失败"
                print(error_message)
                error_file.write(error_message + '\n')

        else:
            error_message = "接口1 - 短信发送失败"
            print(error_message)
            error_file.write(error_message + '\n')

    except Exception as e:
        error_message = f"出现异常：{str(e)}"
        print(error_message)
        error_file.write(error_message + '\n')
